﻿using QD.Tools;
using QD_Clients.Code.DbContent;
using QD_Clients.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QD_Clients
{
    public partial class IconMag : BaseForm
    {
        public IconMag()
        {
            InitializeComponent();

            #region 注册菜单
            base.ToolsBtnAdd = this.toolStripButton4;
            base.ToolsBtnUpdate = this.toolStripButton3;
            base.ToolsBtnDelete = this.toolStripButton2;
            base.ToolsBtnClose = this.toolStripButton1;
            #endregion

            this.gridList.InitGrid();
        }
        private void DataBind_Grid()
        {
            string key = this.txtKey.Text.Trim().ToLower();
            List<Sys_Icon> plList;

            QDResources db = new QDResources();
            if (string.IsNullOrEmpty(key))
            {
                plList = db.Sys_Icon.OrderByDescending(p => p.CreateDate).ToList();
            }
            else
            {
                plList = db.Sys_Icon.Where(p => p.SICode.Contains(key) || p.SIName.Contains(key)).OrderByDescending(p => p.CreateDate).ToList();
            }
            this.gridList.DataSource = plList;
        }

        public override void ToolsBtnAdd_Click(object sender, EventArgs e)
        {
            this.SICode.Text =
                  this.txtID.Text =
                this.SIName.Text = string.Empty;
            this.tabControl1.SelectedIndex = 0;
        }

        protected override void ToolsBtnDelete_Click(object sender, EventArgs e)
        {
            if (PromptHelper.PromptOkCancel("您确认删除当前数据？") == DialogResult.OK)
            {
                string siCode = this.SICode.Text.Trim();
                if (string.IsNullOrEmpty(siCode))
                {
                    PromptHelper.Prompt("当前数据未保存！");
                }
                else
                {
                    QDResources db = new QDResources();
                    bool isExists = db.Sys_Icon.Any(p => p.SICode == siCode);
                    if (!isExists)
                    {
                        PromptHelper.Prompt("编号[" + siCode + "]不存在或已经被删除！");
                    }
                    else {
                        Sys_Icon mod = db.Sys_Icon.First(p => p.SICode == siCode);
                        db.Sys_Icon.Remove(mod);
                        db.SaveChanges();

                        PromptHelper.Prompt("删除成功！");
                        DataBind_Grid();
                        ToolsBtnAdd_Click(sender, e);
                    }
                }
            }
        }

        protected override void ToolsBtnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.SICode.Text.Trim().Length == 0)
                {
                    throw new Exception("编号不能为空！");
                }
                if (this.SIName.Text.Trim().Length == 0)
                {
                    throw new Exception("名称不能为空！");
                }
                QDResources db = new QDResources();
                int id = 0;
                int.TryParse(this.txtID.Text.Trim(),out id);
                Sys_Icon mod = db.Sys_Icon.FirstOrDefault(p => p.ID == id);
                if (mod == null)
                {
                    if (db.Sys_Icon.Any(p => p.SICode == this.SICode.Text.Trim()))
                    {
                        throw new Exception("编号已经存在！");
                    }
                    else if (db.Sys_Icon.Any(p => p.SIName == this.SIName.Text.Trim()))
                    {
                        throw new Exception("名称已经存在！");
                    }
                    mod = new Sys_Icon();
                  
                    mod.CreateDate = DateTime.Now;
                    db.Sys_Icon.Add(mod);
                }
                else
                {
                    if (db.Sys_Icon.Any(p => p.SICode == this.SICode.Text.Trim() && p.SICode != mod.SICode))
                    {
                        throw new Exception("编号已经存在！");
                    }
                    else if (db.Sys_Icon.Any(p => p.SIName == this.SIName.Text.Trim() && p.SIName != mod.SIName))
                    {
                        throw new Exception("名称已经存在！");
                    }
                }
                mod.SICode = this.SICode.Text.Trim();
                mod.SIName = this.SIName.Text.Trim();
                db.SaveChanges();
                PromptHelper.Prompt("保存成功！");
                InitPlan(mod.ID);
            }
            catch (Exception ex)
            {
                PromptHelper.Prompt(ex.Message);
            }
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            DataBind_Grid();
            this.tabControl1.SelectedIndex = 1;
        }

        private void gridList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
             if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                string id = this.gridList.Rows[e.RowIndex].Cells["ListID"].Value.ToString();
                InitPlan(Convert.ToInt32(id));

                this.tabControl1.SelectedIndex = 0;
            }
        }
        private void InitPlan(int id)
        {
            QDResources db = new QDResources();
            Sys_Icon pt = db.Sys_Icon.FirstOrDefault(p => p.ID == id);

            if (pt != null)
            {
                this.SICode.Text = pt.SICode;
                this.SIName.Text = pt.SIName;
                this.txtID.Text = id.ToString();
            }
        }

        private void 复制单元格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gridList.CopyFirstSelectedCellValue();
        }

        private void 复制所有选中单元格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.gridList.CopySelectedCellsValue();
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (PromptHelper.PromptOkCancel("您确认删除所有选中的数据？") == DialogResult.OK)
            {
                try
                {
                    IList<int> selectRowsIndexs = this.gridList.GetSelectRowsIndex();
                    IList<string> SICodes = new List<string>();

                    using (QDResources db = new QDResources())
                    {
                        foreach (int index in selectRowsIndexs)
                        {
                            SICodes.Add(this.gridList.Rows[index].GetCellValueToString("ListSICode"));
                        }
                        DeleteIcon(SICodes.ToArray());
                    }
                    DataBind_Grid();
                }
                catch (Exception ex)
                {
                    PromptHelper.Prompt(ex);
                }
            }
        }
        private void DeleteIcon(params string[] SICodes)
        {
            using (QDResources db = new QDResources())
            {
                foreach (string siCode in SICodes)
                {
                    List<Sys_Icon> subList = db.Sys_Icon.Where(p => p.SICode == siCode).ToList();
                    foreach (Sys_Icon sub in subList)
                    {
                        db.Sys_Icon.Remove(sub);
                    }
                }

                db.SaveChanges();
            }
            PromptHelper.Prompt("删除成功！");
        }

        private void IconMag_Load(object sender, EventArgs e)
        {
            this.gridList.RowTemplate.ContextMenuStrip = contextMenuStrip1;
        }
    }
}
